package handler

import (
	"app-box/middleware"
	"app-box/models"
	"app-box/service"
	filestore "app-box/tools/FileStore"
	"time"

	"github.com/gin-gonic/gin"
)

type AppBoxHandler struct {
}

func (h AppBoxHandler) InitRoute(route gin.IRouter) {
	app := route.Group("app")
	{
		app.POST("upload", middleware.RequireLogin(models.UserTypeUser), H(h.uploadApp))
		app.GET("download_detail", middleware.Limit(12, time.Minute), H(h.GetAppDownloadDetail))
		app.GET("detail", middleware.RequireLogin(models.UserTypeUser), H(h.GetAppDetail))
		app.GET("list", middleware.RequireLogin(models.UserTypeUser), H(h.getAppList))
		app.POST("appbox/custom_id", middleware.RequireLogin(models.UserTypeUser), H(h.UpdateAppBoxCustomID))
		app.POST("appbox/description", middleware.RequireLogin(models.UserTypeUser), H(h.UpdateAppBoxDescription))
		app.POST("appbox/password", middleware.RequireLogin(models.UserTypeUser), H(h.UpdateAppBoxPassword))
	}
}

func (h AppBoxHandler) uploadApp(ctx Context) {
	var form struct {
		Platform     string               `binding:"required,oneof=Android iOS"` // 平台 Android/iOS
		Name         string               `binding:"required"`                   // app显示名称
		PackageName  string               `binding:"required"`                   // 安卓app包名，iOS为 bundle ID
		Version      string               `binding:"required"`                   // 安装包文件内配置的版本号信息，对应iOS：version，Android：version name
		BuildVersion string               `binding:"required"`                   // 安装包文件内配置的编译版本信息，对应iOS：build version，Android：version code
		Icon         filestore.FileString `binding:"required"`                   // APP图标URl
		VersionTitle string               `binding:"required"`                   // 此次更新版本名称，可自由填写
		UpdateLog    string               // 更新日志，可自由填写
		DownloadURL  filestore.FileString `binding:"required"` // APP 安装包 下载地址
	}
	userID := ctx.GetTokenOrPanic().ID
	ctx.ShouldBindOrPanic(&form)
	service := service.NewAppBoxService()
	appID := service.UploadApp(
		userID,
		form.Platform,
		form.Name,
		form.PackageName,
		form.Version,
		form.BuildVersion,
		form.VersionTitle,
		form.UpdateLog,
		form.DownloadURL,
		form.Icon,
	)
	ctx.Response(map[string]interface{}{
		"AppID": appID,
	})
}

func (h AppBoxHandler) getAppList(ctx Context) {
	userID := ctx.GetTokenOrPanic().ID
	var service = service.NewAppBoxService()
	list := service.GetAppListForUser(userID)
	ctx.Response(list)
}

func (h AppBoxHandler) GetAppDetail(ctx Context) {
	var form struct {
		Platform    string `binding:"required,oneof=Android iOS"`
		PackageName string `binding:"required"`
	}
	ctx.ShouldBindOrPanic(&form)
	var userID = ctx.GetTokenOrPanic().ID
	var service = service.NewAppBoxService()
	box := service.GetAppListForPackage(userID, form.Platform, form.PackageName)
	ctx.Response(box)
}

func (h AppBoxHandler) GetAppDownloadDetail(ctx Context) {
	var form struct {
		ID       string `binding:"required"`
		Password string
	}
	ctx.ShouldBindOrPanic(&form)
	var service = service.NewAppBoxService()
	appBox := service.GetAppBoxForID(form.ID, form.Password)
	ctx.Response(appBox)
}

func (h AppBoxHandler) UpdateAppBoxCustomID(ctx Context) {
	var form struct {
		ID       models.IDType
		CustomID string
	}
	ctx.ShouldBindOrPanic(&form)
	userid := ctx.GetTokenOrPanic().ID

	var service = service.NewAppBoxService()
	service.UpdateAppBoxCustomID(userid, form.ID, form.CustomID)
}

func (h AppBoxHandler) UpdateAppBoxDescription(ctx Context) {
	var form struct {
		ID          models.IDType
		Description string
	}
	ctx.ShouldBindOrPanic(&form)
	userid := ctx.GetTokenOrPanic().ID
	service.NewAppBoxService().UpdateAppBoxDescription(userid, form.ID, form.Description)
}

func (h AppBoxHandler) UpdateAppBoxPassword(ctx Context) {
	var form struct {
		ID       models.IDType
		Password string
	}
	ctx.ShouldBindOrPanic(&form)
	userid := ctx.GetTokenOrPanic().ID
	service.NewAppBoxService().UpdateAppBoxPassword(userid, form.ID, form.Password)
}
